package com.lindroy.jetpacknotes.utils

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding

/**
 * @author Lin
 * @date 2023/12/17
 * @function
 */
inline fun <VB : ViewBinding> FragmentActivity.viewBinding(
    crossinline viewBindingFactory: (LayoutInflater) -> VB
) = lazy { viewBindingFactory(layoutInflater) }

inline fun <F : Fragment, VB : ViewBinding> Fragment.viewBinding(
    crossinline viewBindingFactory: (View) -> VB,
    crossinline viewFactory: (F) -> View = Fragment::requireView
) = FragmentViewBindingProperty { fragment: F ->
    viewBindingFactory(viewFactory(fragment))
}

inline fun <VB : ViewBinding> ViewGroup.viewBinding(
    crossinline viewBindingFactory: (LayoutInflater, ViewGroup, Boolean) -> VB
) = lazy {
    viewBindingFactory(LayoutInflater.from(this.context), this, false)
}